package cn.ll.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import cn.ll.po.Customer;
import cn.ll.po.CustomerDAO;
import cn.ll.po.Permissions;
import cn.ll.po.PermissionsDAO;
import cn.ll.po.RolePermission;
import cn.ll.po.RolePermissionDAO;
import cn.ll.po.User;
import cn.ll.po.UserDAO;
import cn.ll.po.UserRole;
import cn.ll.po.UserRoleDAO;
import cn.ll.util.StringUtil;

public class UserService {

	UserDAO userDAO = null;
	UserRoleDAO userRoleDAO = null;
	RolePermissionDAO rolePermissionDAO = null;
	PermissionsDAO	permissionsDAO = null;
	
	public Map read(Map condition) {
		Map map = null;
		if (condition.get("id") == null) {
			Integer start = (Integer) condition.get("start");
			Integer limit  = (Integer) condition.get("limit");
			if(start != null && limit != null){
				map = this.userDAO.findUserNotDel();
			}
			else{
				map = this.userDAO.findUserNotDel(start,limit);
			}
		} else {
			User user = this.userDAO.findById((String) condition.get("id"));
			List list = new ArrayList();
			list.add(user);
			map = new HashMap();
			map.put("list", list);
			map.put("totalcount", list.size());
		}

		return map;
	}

	public User read(String userid) {
		List ls = null;
		User user = this.userDAO.findById(userid);
		return user;
	}

	/**
	 * 用户登录
	 * 
	 * @param uname
	 * @param password
	 * @return
	 */
	public User login(String uname, String password) {
		User user = new User();
		user.setUsername(uname);
		user.setPassword(StringUtil.sha1(password));
		List<User> lst = userDAO.findByExample(user);
		if (lst.size() == 1) {
			return lst.get(0);
		} else {
			return null;
		}
	}

	public Map create(HttpServletRequest request, HttpServletResponse response) {
		String uname = request.getParameter("username");
		String password = request.getParameter("password");
		String departmentid = request.getParameter("departmentid");
		String role_itemlist = request.getParameter("itemlist");
		
		
		
		User user = new User();
		user.setUsername(uname);

		user.setPassword(StringUtil.sha1(password));
		user.setDepartmentid(departmentid);
		user.setStatus(0);
		this.userDAO.attachDirty(user);
		
		JSONArray jsa = JSONArray.fromObject(role_itemlist);
		if(jsa != null){
			int len = jsa.size();
			for(int i = 0; i < len; i++){
				JSONObject jso = jsa.getJSONObject(i);
				String roleid = jso.getString("id");
				//生成用户角色对象
				UserRole userRole = new UserRole();
				userRole.setRoleid(roleid);
				userRole.setUid(user.getId());
				userRoleDAO.save(userRole);
			}
			Map rs = new HashMap();
			rs.put("success", true);
			return rs;
		}
		Map rs = new HashMap();
		rs.put("success", false);
		return rs;
	}
	
	/**
	 * 根据用户的ID,查询用户的Permission Map
	 * key:name_action
	 * value:id
	 * @param uid
	 * @return
	 */
	public Map usrPermissions(String uid){
		List<UserRole> list = userRoleDAO.findByUid(uid);
		Map map = new HashMap(0);
		if(list != null){
			int size = list.size();
			for(int i = 0; i < size; i++){
				UserRole  userRole = list.get(i);
				List<RolePermission> list_rp = rolePermissionDAO.findByRoleid(userRole.getRoleid());
				
				if(list_rp != null){
					int size_rp = list_rp.size();
					for(int j = 0; j< size_rp; j++){
						RolePermission rolePermission = list_rp.get(j);
						Permissions permissions = permissionsDAO.findById(rolePermission.getPermid());
						String key = permissions.getName()+"_"+permissions.getAction();
						String value = uid;
						map.put(key, value);
					}
				}
			}
		}
		return map;
	}
	
	/**
	 * 根据用户的Id,获取 权限列表 //
	 * @param uid
	 * @return
	 */
	public List<Permissions> usrPermissionsList(String uid){
		List<UserRole> list = userRoleDAO.findByUid(uid);
		List<Permissions> usrPerList = new ArrayList();
		if(list != null){
			int size = list.size();
			for(int i = 0; i < size; i++){
				UserRole  userRole = list.get(i);
				List<RolePermission> list_rp = rolePermissionDAO.findByRoleid(userRole.getRoleid());
				
				if(list_rp != null){
					int size_rp = list_rp.size();
					for(int j = 0; j< size_rp; j++){
						RolePermission rolePermission = list_rp.get(j);
						Permissions permissions = permissionsDAO.findById(rolePermission.getPermid());
						usrPerList.add(permissions);
					}
				}
			}
		}
		return usrPerList;
	}
	
	/**
	 * 根据用户的Id,和权限名称,权限动作,获取 权限列表 //
	 * @param uid
	 * @return
	 */
	public List<Permissions> usrPermissionsList(String uid,String permission,String action){
		List<UserRole> list = userRoleDAO.findByUid(uid);
		List<Permissions> usrPerList = new ArrayList();
		if(list != null){
			int size = list.size();
			for(int i = 0; i < size; i++){
				UserRole  userRole = list.get(i);
				List<RolePermission> list_rp = rolePermissionDAO.findByRoleid(userRole.getRoleid());
				
				if(list_rp != null){
					int size_rp = list_rp.size();
					for(int j = 0; j< size_rp; j++){
						RolePermission rolePermission = list_rp.get(j);
						Permissions permissions = permissionsDAO.findById(rolePermission.getPermid());
						if(permissions.getName().equalsIgnoreCase(permission) && permissions.getAction().equalsIgnoreCase(action)){
							usrPerList.add(permissions);
						}
					}
				}
			}
		}
		return usrPerList;
	}
	
	
	
	public User findById(String id){
		User user = this.userDAO.findById(id);
		return user;
	}
	
	public boolean updateUser(User user) {
		boolean result = this.userDAO.update(user);
		return result;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public Map delete(List<User> userList) {
		for (User user : userList) {

			user = this.userDAO.findById(user.getId());
			user.setStatus(3);
			this.userDAO.attachDirty(user);
		}

		Map rs = new HashMap();
		rs.put("success", true);
		return rs;
	}

	public UserRoleDAO getUserRoleDAO() {
		return userRoleDAO;
	}

	public void setUserRoleDAO(UserRoleDAO userRoleDAO) {
		this.userRoleDAO = userRoleDAO;
	}

	public RolePermissionDAO getRolePermissionDAO() {
		return rolePermissionDAO;
	}

	public void setRolePermissionDAO(RolePermissionDAO rolePermissionDAO) {
		this.rolePermissionDAO = rolePermissionDAO;
	}

	public PermissionsDAO getPermissionsDAO() {
		return permissionsDAO;
	}

	public void setPermissionsDAO(PermissionsDAO permissionsDAO) {
		this.permissionsDAO = permissionsDAO;
	}

	public Map readCbb(Map parameterMap) {
		
		String departmentid = (String)parameterMap.get("departmentid");
		List<User> userLs = userDAO.findByDepartmentid(departmentid);
		List<Map> userCbbLs = new ArrayList<Map>();
		for (User user : userLs) {
			Map mp = new HashMap();
			mp.put("key", user.getId());
			mp.put("value", user.getUsername());
			
			userCbbLs.add(mp);
		}
		
		Map userCbbMp = new HashMap();
		userCbbMp.put("items", userCbbLs);
		
		return userCbbMp;
	}

}
